package com.cxyzy.basic_libs.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
import java.util.*

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class FrameView(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {
    private var mPaint: Paint = Paint()
    var path: Path = Path()

    init {
        mPaint.color = Color.parseColor("#fab27b")
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.STROKE;
        mPaint.strokeWidth = 10f
        start()
    }

    private var mTimer: Timer? = null

    private fun start() {
        mTimer = Timer()
        mTimer?.schedule(object : TimerTask() {
            override fun run() {
                //工作线程中用postInvalidate(); UI线程用invalidate()
                postInvalidate()
            }
        }, 50, 50)

    }

    override fun onDraw(canvas: Canvas) {
        drawRect()
        canvas.drawPath(path, mPaint)
    }

    private var padding = 0
    private fun drawRect() {
        path.moveTo(0f + padding, 0f + padding)
        path.lineTo(width.toFloat() - padding, 0f + padding)
        path.lineTo(width.toFloat() - padding, height.toFloat() - padding)
        path.lineTo(0f + padding, height.toFloat() - padding)
        path.lineTo(0f + padding, 0f + padding)
        padding += 30
    }
}